﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace car_o_matic
{
    public partial class frmAddVehicle : Form
    {
        Vehicle tmpVehicle;

        public bool valid { get; set; }

        public frmAddVehicle()
        {
            InitializeComponent();
            cbType.Items.Add("Car");
            cbType.Items.Add("Truck");
            cbType.Items.Add("Bus");
        }

        public Vehicle getVehicle
        {
            get
            {
                return tmpVehicle;
            }
        }
        
        private void Add_Click(object sender, EventArgs e)
        {
            //set to true for new validation attempt
            valid = true;
            if (cbType.SelectedItem.ToString() == "")
            {
                valid = false;
                MessageBox.Show("Please select a vehicle type");
            }
            tmpVehicle = (Vehicle)System.Activator.CreateInstance(Type.GetType("car_o_matic." + cbType.SelectedItem.ToString()));
            tmpVehicle.name = name.Text;
            try
            {
                tmpVehicle.address = int.Parse(address.Text);
            }
            catch (Exception ex)
            {
                valid = false;
                MessageBox.Show("DCC Address has to be nummeric");
            }
            tmpVehicle.description = description.Text;

            if (valid)
            {
                //empty form
                name.Text = "";
                address.Text = "";
                description.Text = "";
            }
        }
    }
}
